package com.ruoyi.accounting.controller;

import com.ruoyi.accounting.domain.AccAccount;
import com.ruoyi.accounting.domain.AccClassification;
import com.ruoyi.accounting.domain.dto.AccAccountDTO;
import com.ruoyi.accounting.service.AccAccountService;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 账户Controller
 *
 * @author dpf
 * @date 2025-07-17
 */
@RestController
@RequestMapping("/account")
public class AccAccountController extends BaseController {

    @Autowired
    private AccAccountService accAccountService;

    /**
     * 查询账户列表
     */
    @RequiresPermissions("system:account:list")
    @GetMapping("/list")
    public TableDataInfo list(AccAccount accAccount) {
        startPage();
        List<AccAccount> list = accAccountService.selectAccAccountList(accAccount);
        return getDataTable(list);
    }

    /**
     * 导出账户列表
     */
    @RequiresPermissions("system:account:export")
    @Log(title = "账户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AccAccount accAccount) {
        List<AccAccount> list = accAccountService.selectAccAccountList(accAccount);
        ExcelUtil<AccAccount> util = new ExcelUtil<AccAccount>(AccAccount.class);
        util.exportExcel(response, list, "账户数据");
    }

    /**
     * 获取账户详细信息
     */
    @RequiresPermissions("system:account:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(accAccountService.selectAccAccountById(id));
    }

    /**
     * 新增账户
     */
    @RequiresPermissions("system:account:add")
    @Log(title = "账户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Valid AccAccountDTO accAccountDTO) {
        AccAccount accAccount = new AccAccount();
        BeanUtils.copyBeanProp(accAccount, accAccountDTO);
        return toAjax(accAccountService.insertAccAccount(accAccount));
    }

    /**
     * 修改账户
     */
    @RequiresPermissions("system:account:edit")
    @Log(title = "账户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AccAccount accAccount) {
        return toAjax(accAccountService.updateAccAccount(accAccount));
    }

    /**
     * 删除账户
     */
    @RequiresPermissions("system:account:remove")
    @Log(title = "账户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(accAccountService.deleteAccAccountByIds(ids));
    }
}
